var pageNum;
$(function () {

    getQuestionList(1);
    $('body').on('click','#btn1,#btn2', function () {//条件搜索
        getQuestionList(1);
    }).on('click','#enable-batch',function () {//批量恢复
        var obj = $('input[name=questionId]:checked').parents('tr');
        updateQuestionStatus(obj, 1);
    }).on('click','.fa-history',function () {//单个恢复
        var obj = $(this).parents('tr');
        updateQuestionStatus(obj, 1);
    }).on('click','#disable-batch',function () {//批量禁用
        var obj = $('input[name=questionId]:checked').parents('tr');
        updateQuestionStatus(obj, 2);
    }).on('click','.fa-unlock-alt',function () {//单个禁用
        var obj = $(this).parents('tr');
        updateQuestionStatus(obj, 2);
    }).on('click','#delete-batch',function () {//批量删除
        var obj = $('input[name=questionId]:checked').parents('tr');
        updateQuestionStatus(obj, 3);
    }).on('click','.fa-trash-o',function () {//单个删除
        var obj = $(this).parents('tr');
        updateQuestionStatus(obj, 3);
    })

    /**
     * 查询问题列表
     * @param pageNo
     */
    function getQuestionList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/qa/question/ajaxQueryQuestionList',//请求URLt
            contextId: 'oc-ibox-content',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getQuestionList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-qaTag', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                $("#question-count").html(total);
                $('[data-toggle="tooltip"]').tooltip();
                $("[data-toggle=popover]").popover();
            }
        };
        ajaxGetListData(obj);
    }
    
    function updateQuestionStatus(obj, status) {
        var message = "";
        if(status==1){
            message = '恢复';
        }else if(status == 2){
            message = '禁用';
        }else if(status == 3 ){
            message = '删除';
        }

        if($.isEmptyObject(obj) || obj.length <= 0){
            icModale(0, '请选择要'+ message +'的数据');return;
        }
        icModale(2,"确定要"+ message +"所选的问题吗?",3,function () {
            var questionIds = obtainIds(obj);
            $.ajax({
                url: baseloaction + '/admin/qa/question/updateQuestionStatus',
                data:{"questionIds": questionIds, "status":status},
                type:"post",
                dataType:"json",
                success: function (result) {
                    icModale(0, result.message);
                    if(result.success){
                        if(status == 1){
                            obj.find('td[questionStatus]').html('<span class="label label-info">正常</span>');
                            obj.find('a[questionStatus]').attr('data-original-title','禁用').html('<i class="fa fa-unlock-alt"></i>');
                        }else if(status == 2){
                            obj.find('td[questionStatus]').html('<span class="label label-danger">禁用</span>');
                            obj.find('a[questionStatus]').attr('data-original-title','恢复').html('<i class="fa fa-history"></i>');
                        }else if(status == 3){
                            getQuestionList(pageNum);
                        }
                    }
                },
                error: function (error) {
                    icModale(0, '系统繁忙，数据'+ message +'失败');
                }
            })
        });
    }


    //拼接id串
    function obtainIds(obj) {
        if(null == obj || obj.size() == 0){
            icModale(0,"请选择要操作的数据");
            return null;
        }
        var questionIds = "";
        $(obj).each(function (index,_input) {
            questionIds += $(_input).data('id') + ",";
        })
        questionIds = questionIds.substring(0,questionIds.length-1);
        return questionIds;
    }
});

